<?php
/**
 * TRC20线上充值记录
 */
namespace app\common\model;

use think\Model;

class RechargeOrder extends Model
{
    protected $name = 'recharge_order';

    /**
     * TRC20充值记录
     * @param $mid  用户ID
     * @param $maid 账号ID
     * @param $hash 交易hash
     * @param $amount   充值金额
     ** @param $to_address   充值用户
     * @param $currency 币种
     * @return int|string
     */
    public static function add($mid,$maid,$hash,$amount,$to_address,$currency = 'USDT'){
        $data = [
            'mid'   =>  $mid,
            'maid'  =>  $maid,
            'hash'  =>  $hash,
            'amount'    =>  $amount,
            'currency'  =>  $currency,
            'to_address'    =>  $to_address,
            'add_time'  =>  date('Y-m-d H:i:s',time())
        ];
        $id = self::insertGetId($data);
        return $id;
    }

    /**
     * 记录转入手续费时间
     * @param $id   充值记录ID
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function service_charge($id){
        $data = self::where('id',$id)->where('type',1)->find();
        if($data){
            self::where('id',$id)->where('type',1)->update(['service_time'=>time()]);
            return true;
        }
        return false;
    }

    /**
     * 根据指定哈希值查询详情
     * @param $hash 哈希值
     * @return RechargeOrder|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getHash($hash){
        return self::where('hash',$hash)->find();
    }

    /**
     * 查询归集信息
     * @param int $page 分页
     * @param int $limit 每页数量
     * @param array $where 查询条件
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getList($page=1,$limit=20,$where=[],$order = 'id desc'){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $data;
    }
}